package com.yy.finvideoplayer.renderview

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.util.Log
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import com.yy.finvideoplayer.media.IMediaControl

/**
 * @Description: SufaceRenderView
 * @Author: yiw89
 * @CreateDate: 2025/9/1 14:53
 * @Version: 1.0
 */
class SufaceRenderView : SurfaceView, SurfaceHolder.Callback,
    IRenderView {
    private lateinit var mMeasureHelper: MeasureHelper
    private var videoWidth = 0
    private var videoHeight = 0

    private var callback: IRenderView.IRenderCallback? = null

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    private fun init() {
        getHolder().addCallback(this)
        //getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        mMeasureHelper = MeasureHelper(this)
    }


    override fun get(): View {
        return this
    }

    override fun setVideoSize(videoWidth: Int, videoHeight: Int) {
        if (videoWidth >= 0 && videoHeight >= 0) {
            this.videoWidth = videoWidth
            this.videoHeight = videoHeight
            if (holder != null) holder!!.setFixedSize(videoWidth, videoHeight)
            mMeasureHelper.setVideoSize(videoWidth, videoHeight)

            requestLayout()
        }
    }

    override fun setAspectRatio(aspectRatio: Int) {
        mMeasureHelper.setAspectRatio(aspectRatio)
        requestLayout()
    }

    override fun setVideoRotation(degree: Int) {
        //不支持
    }

    override fun addRenderCallback(callback: IRenderView.IRenderCallback?) {
        this.callback = callback
    }

    override fun removeRenderCallback() {
        this.callback = null
    }


    override fun getSurfaceHolder(): SurfaceHolder? {
        return holder
    }

    override fun openSurface(): Surface? {
        if (holder != null) return holder!!.surface
        return null
    }

    override fun bindMedia(iMediaControl: IMediaControl) {
        //iMediaControl.setDisplay(null);
        holder!!.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
        getSurfaceHolder()?.let { iMediaControl.setDisplay(it) }
    }

    override fun getCurrentFrame(): Bitmap? {
        return null
    }

    private var holder: SurfaceHolder? = null


    override fun surfaceCreated(holder: SurfaceHolder) {
        Log.i(TAG, "surfaceCreated")
        this.holder = holder
        callback?.onSurfaceCreated(this, 0, 0)
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        Log.i(TAG, "surfaceChanged $width-$height")
        callback?.onSurfaceChanged(this, format, width, height)
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        Log.i(TAG, "surfaceDestroyed")
        callback?.onSurfaceDestroyed(this)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(mMeasureHelper.getMeasuredWidth(), mMeasureHelper.getMeasuredHeight())
    }


    companion object {
        protected const val TAG: String = "SufaceRenderView"
    }
}